package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【room_info(房间信息表)】的数据库操作Service实现
* @createDate 2025-01-16 16:38:23
*/
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
    implements RoomInfoService{

	@Autowired
	private RoomLeaseTermService roomLeaseTermService;

	@Autowired
	private RoomPaymentTypeService roomPaymentTypeService;

	@Autowired
	private RoomAttrValueService roomAttrValueService;

	@Autowired
	private RoomLabelService roomLabelService;

	@Autowired
	private RoomFacilityService roomFacilityService;

	@Autowired
	private GraphInfoService graphInfoService;

	@Autowired
	private RoomInfoMapper roomInfoMapper;

	@Autowired
	private ApartmentInfoMapper apartmentInfoMapper;

	@Autowired
	private GraphInfoMapper graphInfoMapper;

	@Autowired
	private AttrValueMapper attrValueMapper;

	@Autowired
	private FacilityInfoMapper facilityInfoMapper;

	@Autowired
	private LabelInfoMapper labelInfoMapper;

	@Autowired
	private PaymentTypeMapper paymentTypeMapper;

	@Autowired
	private LeaseTermMapper leaseTermMapper;
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;


	@Override
	public void saveOrUpdateByRoom(RoomSubmitVo roomSubmitVo) {
		// 调用的是RoomInfo的saveOrUpdate，所以只能自动处理RoomSubmitVo父类（RoomInfo）中的属性值
		// RoomSubmitVo中的属性则需要我们自己进行处理
		boolean isUpdate = roomSubmitVo.getId() != null;
		super.saveOrUpdate(roomSubmitVo);
		// 判断传入id值是否为空，为空则直接进行插入，不为空则先进行删除再进行插入
		if (isUpdate){
			// 删除房间租期关系表
			LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
			roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
			roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

			// 删除房间支付关系表
			LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
			roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
			roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

			// 删除房间属性值关系表
			LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
			roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
			roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

			// 删除房间标签关系表
			LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
			roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
			roomLabelService.remove(roomLabelLambdaQueryWrapper);

			// 删除房间配套关系表
			LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
			roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
			roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

			// 删除图片信息表
			LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
			graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
			graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
			graphInfoService.remove(graphInfoLambdaQueryWrapper);

			// 删除缓存
			String key = RedisConstant.APP_ROOM_PREFIX+roomSubmitVo.getId();
			redisTemplate.delete(key);

		}
		// 插入数据
		// 房间租期关系表
		// 获取传入的租期标签id
		List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
		// 判断传入的id值是否为空
		if(!CollectionUtils.isEmpty(leaseTermIds)){
			// new ArrayList
			ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
			// for循环将全部的房间标签id取出
			for (Long leaseTermId : leaseTermIds){
				RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
				roomLeaseTerm.setLeaseTermId(leaseTermId);
				roomLeaseTerm.setRoomId(roomSubmitVo.getId());
				roomLeaseTerms.add(roomLeaseTerm);
			}
			roomLeaseTermService.saveBatch(roomLeaseTerms);
		}

		// 房间支付关系表
		// 获取传入的支付方式id
		List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
		// 判断传入的id值是否为空
		if(!CollectionUtils.isEmpty(paymentTypeIds)){
			ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
			for (Long paymentTypeId : paymentTypeIds){
				RoomPaymentType roomPaymentType = new RoomPaymentType();
				roomPaymentType.setRoomId(roomSubmitVo.getId());
				roomPaymentType.setPaymentTypeId(paymentTypeId);
				roomPaymentTypes.add(roomPaymentType);
			}
			roomPaymentTypeService.saveBatch(roomPaymentTypes);
		}

		// 房间属性值关系表
		List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
		if(!CollectionUtils.isEmpty(attrValueIds)){
			ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
			for (Long attrValueId : attrValueIds){
				RoomAttrValue roomAttrValue = new RoomAttrValue();
				roomAttrValue.setRoomId(roomSubmitVo.getId());
				roomAttrValue.setAttrValueId(attrValueId);
				roomAttrValues.add(roomAttrValue);
			}
			roomAttrValueService.saveBatch(roomAttrValues);
		}

		// 房间标签关系表
		List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
		if(!CollectionUtils.isEmpty(labelInfoIds)){
			ArrayList<RoomLabel> roomLabels = new ArrayList<>();
			for (Long labelInfoId : labelInfoIds){
				RoomLabel roomLabel = new RoomLabel();
				roomLabel.setRoomId(roomSubmitVo.getId());
				roomLabel.setLabelId(labelInfoId);
				roomLabels.add(roomLabel);
			}
			roomLabelService.saveBatch(roomLabels);
		}

		// 房间配套关系表
		List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
		if(!CollectionUtils.isEmpty(facilityInfoIds)){
			ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
			for (Long facilityInfoId : facilityInfoIds){
				RoomFacility roomFacility = new RoomFacility();
				roomFacility.setRoomId(roomSubmitVo.getId());
				roomFacility.setFacilityId(facilityInfoId);
				roomFacilities.add(roomFacility);
			}
			roomFacilityService.saveBatch(roomFacilities);
		}

		// 图片信息表
		List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
		if(!CollectionUtils.isEmpty(graphVoList)){
			ArrayList<GraphInfo> graphInfos = new ArrayList<>();
			for (GraphVo graphVo : graphVoList){
				GraphInfo graphInfo = new GraphInfo();
				graphInfo.setName(graphVo.getName());
				graphInfo.setItemType(ItemType.ROOM);
				graphInfo.setItemId(roomSubmitVo.getId());
				graphInfo.setUrl(graphVo.getUrl());
				graphInfos.add(graphInfo);
			}
			graphInfoService.saveBatch(graphInfos);
		}

	}

	@Override
	public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
		return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
	}

	@Override
	public void removeRoomById(Long id) {
		//1.删除RoomInfo
		super.removeById(id);

		//2.删除graphInfoList
		LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
		graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
		graphQueryWrapper.eq(GraphInfo::getItemId, id);
		graphInfoService.remove(graphQueryWrapper);

		//3.删除attrValueList
		LambdaQueryWrapper<RoomAttrValue> attrQueryWrapper = new LambdaQueryWrapper<>();
		attrQueryWrapper.eq(RoomAttrValue::getRoomId, id);
		roomAttrValueService.remove(attrQueryWrapper);

		//4.删除facilityInfoList
		LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
		facilityQueryWrapper.eq(RoomFacility::getRoomId, id);
		roomFacilityService.remove(facilityQueryWrapper);

		//5.删除labelInfoList
		LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
		labelQueryWrapper.eq(RoomLabel::getRoomId, id);
		roomLabelService.remove(labelQueryWrapper);

		//6.删除paymentTypeList
		LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
		paymentQueryWrapper.eq(RoomPaymentType::getRoomId, id);
		roomPaymentTypeService.remove(paymentQueryWrapper);

		//7.删除leaseTermList
		LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>();
		termQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
		roomLeaseTermService.remove(termQueryWrapper);

		// 删除缓存
		String key = RedisConstant.APP_ROOM_PREFIX+id;
		redisTemplate.delete(key);
	}

	@Override
	public RoomDetailVo getRoomDetailById(Long id) {

		//1.查询RoomInfo
		RoomInfo roomInfo = roomInfoMapper.selectById(id);
		System.out.println("roomInfo = " + roomInfo);

		//2.查询所属公寓信息
		ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

		//3.查询graphInfoList
		List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);


		//4.查询attrValueList
		List<AttrValueVo> attrvalueVoList = attrValueMapper.selectListByRoomId(id);


		//5.查询facilityInfoList
		List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);


		//6.查询labelInfoList
		List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);


		//7.查询paymentTypeList
		List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);

		//8.查询leaseTermList
		List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);


		RoomDetailVo adminRoomDetailVo = new RoomDetailVo();
		BeanUtils.copyProperties(roomInfo, adminRoomDetailVo);

		adminRoomDetailVo.setApartmentInfo(apartmentInfo);
		adminRoomDetailVo.setGraphVoList(graphVoList);
		adminRoomDetailVo.setAttrValueVoList(attrvalueVoList);
		adminRoomDetailVo.setFacilityInfoList(facilityInfoList);
		adminRoomDetailVo.setLabelInfoList(labelInfoList);
		adminRoomDetailVo.setPaymentTypeList(paymentTypeList);
		adminRoomDetailVo.setLeaseTermList(leaseTermList);

		return adminRoomDetailVo;
	}


}




